/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.dao.impl;

import com.legendshop.dao.impl.GenericDaoImpl;
import com.legendshop.dao.support.CriteriaQuery;
import com.legendshop.dao.support.EntityCriterion;
import com.legendshop.dao.support.PageSupport;
import com.legendshop.dao.sql.ConfigCode;
import com.legendshop.oa.dao.AttachmentTreeDao;
import com.legendshop.oa.model.AttachmentTree;
import com.legendshop.util.AppUtils;

import java.util.List;

/**
 * The Class AttachmentTreeDaoImpl.
 */

public class AttachmentTreeDaoImpl extends GenericDaoImpl<AttachmentTree, Long> implements AttachmentTreeDao  {
     
    public List<AttachmentTree> getAttachmentTree(String userName){
   		return this.queryByProperties(new EntityCriterion().eq("userName", userName));
    }

	public AttachmentTree getAttachmentTree(Long id){
		return getById(id);
	}
	
    public int deleteAttachmentTree(AttachmentTree attachmentTree){
    	return delete(attachmentTree);
    }
	
	public Long saveAttachmentTree(AttachmentTree attachmentTree){
		return save(attachmentTree);
	}
	
	public int updateAttachmentTree(AttachmentTree attachmentTree){
		return update(attachmentTree);
	}
	
	public PageSupport getAttachmentTree(CriteriaQuery cq){
		return queryPage(cq);
	}

	@Override
	public List<AttachmentTree> getAttachmentTreeByPid(Long pId, String userName) {
		return this.queryByProperties(new EntityCriterion().eq("userName", userName).eq("parentId", pId));
	}

	@Override
	public void updateAttmntTreeNameById(Integer id, String name) {
		this.update("update ls_attmnt_tree set name=? where id=?", name,id);
	}

	@Override
	public AttachmentTree getAttachmentTree(Long treeId, Long shopId) {
		List<AttachmentTree> attmntList =  this.queryByProperties(new EntityCriterion().eq("id", treeId).eq("shopId", shopId));
		if(AppUtils.isNotBlank(attmntList)){
			return attmntList.get(0);
		}else{
			return null;
		}
	}

	/**
	 * 查找所有子节点树
	 */
	@Override
	public List<AttachmentTree> getAllChildByTreeId(Long treeId) {
		String sql = ConfigCode.getInstance().getCode(this.getDialect().getDialectType() + ".getAllChildByTreeId"); //由于采用了递归算法，因此不同的数据库会有不同的sql实现, 见dialectSQL.dal.xml
		return this.query(sql, AttachmentTree.class, treeId);
	}
	
 }
